package com.dragon.app.controller.business;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.app.config.annotation.AddPage;
import com.dragon.app.config.annotation.Log;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.contract.ContractService;
import com.dragon.repository.domain.dto.ContractDTO;
import com.dragon.repository.domain.dto.CustomerDTO;
import com.dragon.repository.domain.dto.QuotesDTO;
import com.dragon.repository.domain.vo.ContractVO;
import com.dragon.repository.domain.vo.QuotesVO;
import com.dragon.repository.entity.Contract;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Charlotte
 */
@RestController
@Api(tags = "合同")
@RequestMapping("contract")
public class ContractController {

    @Autowired
    ContractService service;


    @GetMapping("list")
    @ApiOperation("合同列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true,  type = "int"),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true,  type = "int"),
            @ApiImplicitParam(name = "search", value = "搜索条件", required = false,  type = "string"),
            @ApiImplicitParam(name = "status", value = "审核状态", required = false, type = "int"),
            @ApiImplicitParam(name = "applicationId", value = "业务员id", required = false, type = "int"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, type = "string"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, type = "string"),
            @ApiImplicitParam(name = "customerId", value = "客户id", required = false, type = "int")
    })
    public ResponseData<List<ContractDTO>> contractList(
            @RequestParam Integer page,
            @RequestParam Integer limit,
            @RequestParam(required = false) String search,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) Integer applicationId,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) Integer customerId
    ){
        Page<Contract> contractPage = service.contractList(page, limit, search, status, applicationId, startTime, endTime, customerId);
        return ResponseData.success(ContractDTO.convertList(contractPage.getRecords()), contractPage.getTotal());
    }


    @PostMapping("add")
    @ApiOperation("添加合同")
    @Log
    @AddPage
    public ResponseData addContract(@RequestBody @Validated(Insert.class) ContractVO vo) {
        Contract contract = ContractVO.convert(vo);
        contract.setQuotesData(QuotesVO.convert(vo.getQuotesData()));
        return ResponseData.success(service.addContract(contract));
    }

    @PostMapping("update")
    @ApiOperation("修改合同")
    @Log
    public ResponseData updateContract(@RequestBody @Validated(Update.class) ContractVO vo ){
        Contract contract = ContractVO.convert(vo);
        contract.setQuotesData(QuotesVO.convert(vo.getQuotesData()));
        service.updateContract(contract);
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation("删除合同")
    @Log
    public ResponseData deleteContract(@RequestParam @ApiParam(name = "ids", value = "合同id集合", required = true,  type = "array") List<Integer> ids){
        service.deleteContract(ids);
        return ResponseData.success();
    }

    @PostMapping("contractById")
    @ApiOperation("根据id查看合同详情")
    public ResponseData viewById(@ApiParam(name = "id", value = "合同id", required = true, type = "int") @RequestParam Integer id){
        Contract contracts = service.selectByContractId(id);
        ContractDTO dto = ContractDTO.convert(contracts);
        dto.setQuotesData(QuotesDTO.convert(contracts.getQuotesData()));
        dto.setCustomerData(CustomerDTO.convert(contracts.getCustomerData()));
        return ResponseData.success(dto);
    }

    @PostMapping("progress")
    @ApiOperation("获取合同进度")
    public ResponseData getContractProgress(@ApiParam(name = "id", value = "合同id", required = true,  type = "int")
                                            @RequestParam Integer id){
        return ResponseData.success(service.getContractSchedule(id,null));
    }

    @GetMapping("statistics")
    @ApiOperation("业务统计信息")
    public ResponseData businessStatistics(){
        return ResponseData.success(service.businessStatistics());
    }

    @GetMapping("contractAllInformation")
    @ApiOperation(value = "合同总信息")
    public ResponseData<ContractDTO> contractAllInformation(
            @ApiParam(name = "contractId", value = "合同id", required = true, type = "int")
            @RequestParam Integer contractId){
        Contract contract = service.selectByContractId(contractId);
        return ResponseData.success(ContractDTO.convert(contract));
    }
}
